package com.hnkywl.sts.service.xsgl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.base.SjzdzDao;
import com.hnkywl.sts.dao.xsgl.KhDao;
import com.hnkywl.sts.entity.base.Sjzdz;
import com.hnkywl.sts.entity.cggl.Cgd;
import com.hnkywl.sts.entity.jggl.Jgdmx;
import com.hnkywl.sts.entity.organ.Ck;
import com.hnkywl.sts.entity.sys.Gg;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-23
 * Time: 下午2:33
 * To change this template use File | Settings | File Templates.
 */
@Service
public class KhService {
    private KhDao khDao;
    private SjzdzDao sjzdzDao;
    private BmService bmService;

    public String findAllKh(String khbm, String jtwzname, String jtwzggxh, String jtwzbm, Long ckid, Long xsgsId, Long sbgsId, Long cdid, Long wzggid, int pageNo, int pageSize) {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page khPage = this.khDao.findAllKhList(khbm, jtwzname, jtwzggxh, jtwzbm, ckid, xsgsId, sbgsId, cdid, wzggid, pageNo, pageSize);
        String xsdJson = gson.toJson(khPage.getResult());
        return "{\"total\":" + khPage.getTotalCount() + ",\"rows\":" + xsdJson + "}";
    }

    /**
     * 根据日期查询库存
     *
     * @return
     */
    public List findKhGroupGs() {
        try {
            String sql = "select " +
                    "substr((select tg.jc from t_sys_gsjg tg where tg.id = tk.sbgs_id),0,2) as jc," +
                    "sum(tk.sl) as sumsl " +
                    "from t_biz_kh tk, t_sys_gsjg tg " +
                    "where tk.sbgs_id = tg.id and tk.sbgs_id is not null and tk.deleted = 0 " +
                    "group by tk.sbgs_id " +
                    "order by jc asc";
            List slList = this.khDao.findBySQL(sql);
            return slList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Kh findKh(Long khId) {
        Kh kh = this.khDao.get(khId);
        return kh;
    }

    //加工单生成卡号
    @Transactional(propagation = Propagation.REQUIRED)
    public Kh createKhByJgdmxs(Ck ck, Gg gg, String cdcode, List<Jgdmx> jgdmxList, String number, String weight, Double zweight, Double zwsdj, String sku) {
        Double dwsdj = zwsdj / zweight;
        Kh ykh = jgdmxList.get(0).getKh();
        int pxh = this.findPxhByKh(ykh);
        Sjzdz cd = null;
        if (cdcode != null) {
            cd = sjzdzDao.getSjzdzIdByBm(cdcode);
        }
        Kh kh = new Kh();
        kh = (Kh) ReflectionUtils.getInstance(kh, ykh);
        kh.setId(null);
        kh.setKhbm(bmService.getKckhbm(ck.getId()));
        kh.setDateCreated(new Date());
        kh.setLastUpdated(new Date());
        kh.setCd(cd);
        kh.setCk(ck);
        kh.setJtwz(gg);
        kh.setWsdj(dwsdj);
        kh.setPxh(pxh + 1);
        kh.setJs(Integer.parseInt(number));
        kh.setSl(Double.valueOf(weight));
        kh.setKkdsl(Double.valueOf(weight));
        kh.setCshjs(Integer.parseInt(number));
        kh.setCshsl(Double.valueOf(weight));
        kh.setJhjs(Integer.parseInt(number));
        kh.setJhsl(Double.valueOf(weight));
        kh.setJgzt("1");
        kh.setSku(sku);
        kh.setXsmxList(null);
//        kh.setGdList(null);
        khDao.save(kh);
        return kh;
    }

    private int findPxhByKh(Kh kh) {
        Cgd cgd = kh.getCgd();
        int count = khDao.findPxhByCgd(cgd.getId()).intValue();
        return count;
    }

    @Autowired
    public void setKhDao(KhDao khDao) {
        this.khDao = khDao;
    }

    @Transactional
    public void save(Kh kh) {
        this.khDao.save(kh);
    }

    @Autowired
    public void setSjzdzDao(SjzdzDao sjzdzDao) {
        this.sjzdzDao = sjzdzDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }


}
